package cn.sxt.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import cn.sxt.domain.Topic;
import cn.sxt.service.TopicService;
import cn.sxt.utils.DataGridView;
import cn.sxt.vo.TopicVo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lij
 * @since 2018-10-08
 */
@Controller
@RequestMapping("/topic")
public class TopicController {

	@Autowired
	private TopicService topicService;

	@RequestMapping("queryAll")
	public String queryAll(Model model) {
		Wrapper<Topic> wrapper = new EntityWrapper<>();
		List<Topic> list = topicService.selectList(wrapper);
		model.addAttribute("list", list);
		return "../list.jsp";
	}
	
	// 跳到用户管理topicManager.jsp
	@RequestMapping("toTopicManager")
	public String toTopicManager() {
		return "../topicManager.jsp";
	}

	// 用户全查询，分页，模糊
	@RequestMapping("queryAllTopic")
	@ResponseBody
	public DataGridView queryAllTopic(TopicVo topicVo) {
		return topicService.queryAllTopic(topicVo);
	}

	// 添加
	@RequestMapping("addTopic")
	@ResponseBody
	public Map<String, Object> addTopic(TopicVo topicVo) {
		Map<String, Object> map = new HashMap<>();
		try {
			topicVo.setCreatedate(new Date());
			topicVo.setClickamount(0);
			boolean flag = topicService.insert(topicVo);
			// int i = topicService.addTopic(topicVo);
			if (flag) {
				map.put("msg", "添加成功");
			} else {
				map.put("msg", "添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "添加失败");
		}
		return map;
	}

}
